var url = "/openx/logistics/partnerShield/listPartnerShield", formId = 'partnerform', param = {};
// loading
(function () {
    var data = $.extend(param, $.openx.getJsonObj('queryForm'));
    $.openx.initTable("table", url, data);
    $.openx.setTableToolBar('partnerbar'); //设置工具栏
})();

//格式化时间
function formatterDate(v, row) {
    return $.openx.formatDate(new Date(v));
}

function formatterRemark(v, row) {
    return '此配置表示' + row.storeCode + " 门店，不支持" + row.deliveryName;
}


//查询列表
$.openx.query = function () {
    var data = $.extend(param, $.openx.getJsonObj('queryForm'));
    $.openx.tableRefresh(null, data, true)
}

/**按门店屏蔽物流 */
$.openx.insert = function () {
    var innerHtml = $.openx.innerHtml('partnerformtpl');
    //查询物流列表
    $X("/logistics/partner", "listPartner").callx({
        onResult: function (data) {
            if (data && data.rows.length > 0) {
                //组成物流列表串
                var partnerStr = ""
                for (var i = 0; i < data.rows.length; i++) {
                    if (data.rows[i].status === 'ENABLE') {
                        var logisticsId = data.rows[i].logisticsId;
                        partnerStr += '<div class="checkbox checkbox-success checkbox-inline">'
                        partnerStr += '<input type="checkbox" name="deliveryPartnerCode" id="checkbox' + logisticsId + '" value="' + logisticsId + '" >'
                        partnerStr += '<label for="checkbox' + logisticsId + '"> ' + data.rows[i].name + ' </label>'
                        partnerStr += ' </div>'
                    }
                }
                var paramsData = {};
                paramsData.partnerStr = partnerStr;
                laytpl(innerHtml).render(paramsData, function (html) {
                    $.openx.openLayer(['auto', 'auto'], '新增按门店屏蔽物流', html, false, function () {
                        var partnerShield = getFormData();
                        if (!partnerShield) {
                            return;
                        }
                        if ($.openx.validation(formId)) {
                            $X("/logistics/partnerShield", "createPartnerShield").callx({
                                partnerShield: partnerShield,
                                onResult: function (data) {
                                    if (data) {
                                        $.openx.tableRefresh(null, null, true);
                                        layer.closeAll()
                                    } else {
                                        layer.msg("新增失败")
                                    }
                                },
                                onError: function (error) {
                                    var res = JSON.parse(error);
                                    layer.msg(res.message)
                                }
                            });

                        }
                    });
                })



            }
        },
        onError: function (error) {
            var res = JSON.parse(error);
            layer.msg(res.message)
        }
    });

}


function getFormData() {

    //获取全部选中的 checkbox
    var deliveryPartnerCode = ""
    $("input[type='checkbox'][name='deliveryPartnerCode']:checked").each(function (index, item) {
        deliveryPartnerCode += item.value + ","
    });

    if (deliveryPartnerCode == '') {
        layer.msg('请您选择不支持的物流！')
        return null;
    }
    deliveryPartnerCode = deliveryPartnerCode.substr(0, deliveryPartnerCode.length - 1)

    var submitData = $.openx.getJsonObj('partnerform')
    submitData.deliveryPartnerCode = deliveryPartnerCode;


    return submitData;

}


/**
 *  修改物流操作
 */
$.openx.update = function () {
    $.openx.onClickUpdate(function (row) {
        //根据当前选中的门店编码筛选物流信息
        $X("/logistics/partnerShield", "listPartnerShield").callx({
            storeCode: row[0].storeCode,
            page: {},
            onResult: function (data) {
                var updatePartnerStr = ""
                if (data && data.rows.length > 0) {
                    var shieldList = data.rows; //屏蔽列表
                    $X("/logistics/partner", "listPartner").callx({
                        onResult: function (list) {
                            if (list && list.rows.length > 0) {
                                var listPartner = list.rows;//全部物流数据
                                for (var k = 0; k < listPartner.length; k++) {
                                    if (listPartner[k].status === 'ENABLE') {
                                        var logisticsId = listPartner[k].logisticsId;
                                        var checked = false
                                        for (var index = 0; index < shieldList.length; index++) {
                                            if (listPartner[k].logisticsId === shieldList[index].deliveryPartnerCode) {
                                                checked = true
                                                break;
                                            }
                                        }
                                        updatePartnerStr += '<div class="checkbox checkbox-success checkbox-inline">'
                                        if (checked) {
                                            updatePartnerStr += '<input type="checkbox" checked name="deliveryPartnerCode" id="checkbox' + logisticsId + '" value="' + logisticsId + '" >'
                                        } else {
                                            updatePartnerStr += '<input type="checkbox" name="deliveryPartnerCode" id="checkbox' + logisticsId + '" value="' + logisticsId + '" >'
                                        }
                                        updatePartnerStr += '<label for="checkbox' + logisticsId + '"> ' + listPartner[k].name + ' </label>'
                                        updatePartnerStr += ' </div>'
                                    }
                                }
                                var tplData = {}
                                tplData.storeCode = row[0].storeCode
                                tplData.partnerStr = updatePartnerStr;
                                laytpl($.openx.innerHtml('partnerformtpl')).render(tplData, function (html) {
                                    $.openx.openLayer(['auto', 'auto'], '修改按门店屏蔽物流', html, false, function () {
                                        var partnerShield = getFormData();
                                        if (!partnerShield) {
                                            return;
                                        }
                                        $X("/logistics/partnerShield", "updatePartnerShield").callx({
                                            partnerShield: partnerShield,
                                            onResult: function (data) {
                                                if (data) {
                                                    $.openx.tableRefresh(null, null, true);
                                                    layer.closeAll()
                                                } else {
                                                    layer.msg("更新失败")
                                                }
                                            },
                                            onError: function (error) {
                                                var res = JSON.parse(error);
                                                layer.msg(res.message)
                                            }
                                        });



                                    });
                                })
                            }
                        },
                        onError: function (error) {
                            layer.msg("系统错误，请联系管理员");
                        }
                    });
                }
            },
            onError: function (error) {
                layer.msg("系统错误，请联系管理员");
            }
        });
    });
};

//删除操作
$.openx.delete = function () {
    $.openx.onClickDelete(function (rows) {
        $X("/logistics/partnerShield", "removePartnerShield").callx({
            array: $.openx.getArrays('id',rows),
            onResult: function (data) {
                $.openx.tableRefresh(null, null, true);
                layer.msg("删除成功");
            },
            onError: function (error) {
                layer.msg("系统错误，请联系管理员");
            }
        });
    });
};
